<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px;">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6" >
              <a-form-item label="分类名称">
                <a-input v-model="queryParam.categoryName" placeholder="请输入分类名称" allow-clear @keyup.enter.native="handleQuery"/>
              </a-form-item>
            </a-col>
            <a-col :span="6" >
              <a-form-item label="排序">
                <a-input-number v-model="queryParam.treeSort" :min="0" style="width: 100%"/>
              </a-form-item>
            </a-col>
            <a-col :span="6" >
              <a-form-item label="分类类型" prop="categoryType">
                <a-select placeholder="请选择分类类型" v-model="queryParam.categoryType" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in categoryTypeOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: right;">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <biz-product-category-add-form
        v-if="showAddModal"
        ref="bizProductCategoryAddForm"
        :bizProductCategoryOptions="bizProductCategoryOptions"
        :categoryTypeOptions="categoryTypeOptions"
        @ok="getList"
        @select-tree="getTreeSelect"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <biz-product-category-edit-form
        v-if="showEditModal"
        ref="bizProductCategoryEditForm"
        :bizProductCategoryOptions="bizProductCategoryOptions"
        :categoryTypeOptions="categoryTypeOptions"
        @ok="getList"
        @select-tree="getTreeSelect"
        @close="showEditModal = false"
      />
      <advance-table
        :components="drag(columns)"
        :loading="loading"
        title="产品分类"
        rowKey="id"
        @refresh="getList"
        :expandIconColumnIndex="1"
        :columns="columns"
        :data-source="bizProductCategoryList"
        :pagination="false"
        size="middle"
        tableKey="base-bizProductCategory-index-table_grid"
        :defaultExpandedRowKeys="expandedRowKeys"
        :expandedRowKeys="expandedRowKeys"
        :expandIcon="expandIcon"
        @expand="expandNode"
        :indentSize="16"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }">
        <div class="table-operations" slot="button">
          <a-button type="primary" @click="handleAdd" v-hasPermi="['biz:bizProductCategory:add']">
            <a-icon type="plus" />新增
          </a-button>
          <a-button type="" @click="handleExpand">
            <a-icon type="caret-down" />展开
          </a-button>
          <a-button type="" @click="handleCollapse">
            <a-icon type="caret-up" />折叠
          </a-button>
        </div>
        <span slot="categoryType" slot-scope="{record}">
          {{ categoryTypeFormat(record) }}
        </span>
        <span slot="operation" slot-scope="{text, record}">
          <a @click="handleUpdate(record)" v-hasPermi="['biz:bizProductCategory:edit']">
            修改
          </a>
          <a-divider type="vertical" v-hasPermi="['biz:bizProductCategory:add']" />
          <a @click="handleAdd(record)" v-hasPermi="['biz:bizProductCategory:add']">
            添加子节点
          </a>
          <a-divider type="vertical" v-hasPermi="['biz:bizProductCategory:remove']"/>
          <a @click="handleDelete(record)" v-hasPermi="['biz:bizProductCategory:remove']">
            删除
          </a>
        </span>
      </advance-table>
    </a-card>
  </div>
</template>
<script>
import { listBizProductCategory, delBizProductCategory, searchBizProductCategoryList, getInitData, listTree, listTreeExcludeChild } from '@/api/biz/bizProductCategory'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import BizProductCategoryAddForm from '@/views/biz/bizproductcategory/modules/BizProductCategoryAddForm'
import BizProductCategoryEditForm from '@/views/biz/bizproductcategory/modules/BizProductCategoryEditForm'
import allIcon from '@/core/icons'
import tableDragResizeMixin from '@/mixins/tableDragResizeMixin'
export default {
  name: 'BizProductCategory',
  components: {
    AdvanceTable,
    BizProductCategoryAddForm,
    BizProductCategoryEditForm

  },
  mixins:[tableDragResizeMixin],
  data () {
    return {
      showAddModal: false,
      showEditModal: false,
      allIcon,
      iconVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 展开的主键集合
      expandedRowKeys: [],
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 高级搜索 展开/关闭
      advanced: false,
      bizProductCategoryOptions: [],
      // 产品分类表格数据
      bizProductCategoryList: [],
      // 分类类型字典
      categoryTypeOptions: [],
      // 查询参数
      queryParam: {
        categoryName: undefined,

        treeSort: undefined,

        categoryType: undefined

      },
      columns: [
        {
          title: '分类名称',
          dataIndex: 'categoryName',
          ellipsis: true,
          align: 'left',
          width: '20%'
        },
        {
          title: '排序',
          dataIndex: 'treeSort',
          align: 'right',
          width: '20%'
        },
        {
          title: '分类类型',
          dataIndex: 'categoryType',
          scopedSlots: { customRender: 'categoryType' },
          align: 'center',
          width: '20%'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          ellipsis: true,
          align: 'left',
          width: '20%'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          align: 'center',
          width: '20%',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  created () {
    this.getList()
    getInitData('biz_product_category_type').then(response => {
      this.categoryTypeOptions = response.data.biz_product_category_type
    })
  },
  methods: {
    // 展开收缩时需要动态修改展开行集合
    expandNode (expanded, record) {
      if (expanded) {
        this.expandedRowKeys.push(record.id)
      } else {
        this.expandedRowKeys = this.expandedRowKeys.filter(
          function (item) { return item !== record.id }
        )
      }
      if (expanded && record.children.length === 0) {
        this.loading = true
        listBizProductCategory(this.queryParam, record.id, 1).then(response => {
          record.children = response.data
          this.loading = false
        })
      }
    },
    onSelectChange (selectedRowKeys, selectedRows, expandedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
      this.ids = this.selectedRows.map(item => item.id)
    },
    /** 查询产品分类列表 */
    getList () {
      this.expandedRowKeys = []
      this.loading = true
      listBizProductCategory(this.queryParam).then(response => {
        this.expandTree(response.data, 1, this.expandedRowKeys)
        this.bizProductCategoryList = response.data
        this.loading = false
      })
    },
    // 分类类型字典翻译
    categoryTypeFormat (row) {
      if (row.categoryType) {
        return this.selectDictLabel(this.categoryTypeOptions, row.categoryType)
      } else {
        return ''
      }
    },
    /** 搜索按钮操作 */
    handleQuery () {
        if((this.queryParam.categoryName === undefined || this.queryParam.categoryName === '') &&

        (this.queryParam.treeSort === undefined || this.queryParam.treeSort === '') &&

        (this.queryParam.categoryType === undefined || this.queryParam.categoryType === '')) {
        this.expandedRowKeys = []
        this.getList()
      } else {
        this.loading = true
        searchBizProductCategoryList(this.queryParam).then(response => {
            this.expandedRowKeys = []
            if (response.data.length !== 0) {
              this.getAllBizProductCategoryNode(response.data)
              this.bizProductCategoryList = response.data
            } else {
              this.bizProductCategoryList = []
            }
            this.loading = false
          }
        )
      }
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        categoryName: undefined,

        treeSort: undefined,

        categoryType: undefined

      }
      this.handleQuery()
    },
    getAllBizProductCategoryNode (nodes) {
      if (!nodes || nodes.length === 0) {
        return []
      }
      nodes.forEach(node => {
        if (node.children.length !== 0) {
          this.expandedRowKeys.push(node.id)
        }
        return this.getAllBizProductCategoryNode(node.children)
      })
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    handleAdd (record) {
      this.showAddModal = true
      this.$nextTick(() => (
        this.$refs.bizProductCategoryAddForm.handleAdd(record)
      ))
    },
    handleExpand (record) {
      this.expandedRowKeys = []
      this.loading = true
      searchBizProductCategoryList(this.queryParam).then(response => {
        this.expandedRowKeys = []
        if (response.data.length !== 0) {
          this.getAllBizProductCategoryNode(response.data)
          this.bizProductCategoryList = response.data
        } else {
          this.bizProductCategoryList = []
        }
        this.loading = false
      })
    },
    handleCollapse (record) {
      this.expandedRowKeys = []
      this.loading = true
      listBizProductCategory(this.queryParam).then(response => {
        this.expandTree(response.data, 1, this.expandedRowKeys)
        this.bizProductCategoryList = response.data
        this.loading = false
      })
    },
    handleUpdate (record, ids) {
      this.showEditModal = true
      this.$nextTick(() => (
        this.$refs.bizProductCategoryEditForm.handleUpdate(record, ids)
      ))
    },
    /** 查询菜单下拉树结构 */
    getTreeSelect (row) {
      if (row) {
        listTreeExcludeChild(row.id, '99').then(response => {
          this.bizProductCategoryOptions = []
          const bizProductCategory = { id: 0, categoryName: '根节点', children: [] }
          bizProductCategory.children = response.data
          this.bizProductCategoryOptions.push(bizProductCategory)
        })
      } else {
        listTree('0', '99').then(response => {
          this.bizProductCategoryOptions = []
          const bizProductCategory = { id: 0, categoryName: '根节点', children: [] }
          bizProductCategory.children = response.data
          this.bizProductCategoryOptions.push(bizProductCategory)
        })
      }
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      var that = this
      const bizProductCategoryIds = row.id || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        content: '当前选中的数据',
        onOk () {
          return delBizProductCategory(bizProductCategoryIds)
            .then(() => {
              if (row !== null) {
                that.removeTreeNode(that.bizProductCategoryList, row)
              } else {
                 that.onSelectChange([], [], [])
                 that.getList()
              }
              that.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    expandIcon (props) {
      if (props.record.treeLeaf === 'y') {
        return <span style="margin-right:22px"></span>
      } else {
        if (props.expanded) {
          return (
            <a style="color: 'black',margin-right:0px"
               onClick={(e) => {
                 props.onExpand(props.record, e)
               }}
            >
              <a-icon type="caret-down" />
            </a>
          )
        } else {
          return (
            <a style="color: 'black' ,margin-right:0px"
               onClick={(e) => {
                 props.onExpand(props.record, e)
               }}
            >
              <a-icon type="caret-right" />
            </a>
          )
        }
      }
    }
  }
}
</script>
